class_name AttributeReactionDetector
extends Node2D


@onready var _melt_reaction: AttributeReaction = $MeltReaction
@onready var _overload_reaction: AttributeReaction = $OverloadReaction
@onready var _superconduction_reaction: AttributeReaction = $SuperconductionReaction
@onready var _broken_reaction: AttributeReaction = $BrokenReaction
@onready var _target: Character = get_parent()


@export var disabled: bool = false:
    set(value):
        disabled = value
        if disabled:
            process_mode = Node.PROCESS_MODE_DISABLED
        else:
            process_mode = Node.PROCESS_MODE_INHERIT


signal triggered_reaction(info: DamageInfo, type: AttributeReaction.Type)


func _ready() -> void:
    # _target.before_had_damage.connect(_detect_reaction)
    _target.handle_damage_signals.before_calc_receive_damage.connect(_detect_reaction)


## 当目标处于异常状态时，再次受到其他属性的伤害才会触发属性反应
func _detect_reaction(info: DamageInfo) -> void:
    if disabled: return

    var anomaly_detector: AnomalyDetector = _target.get_node("AnomalyDetector")
    if not anomaly_detector or not anomaly_detector.has_anomaly() \
        or info.attribute == anomaly_detector.current_state().get_damage_attr():
        return
    
    var state := anomaly_detector.current_state()
    var type := AttributeReaction.get_type(state.type, info.attribute)

    var reaction: AttributeReaction = null
    match type:
        AttributeReaction.Type.MELT: reaction = _melt_reaction
        AttributeReaction.Type.OVERLOAD: reaction = _overload_reaction
        AttributeReaction.Type.SUPERCONDUCTION: reaction = _superconduction_reaction
        AttributeReaction.Type.BROKEN: reaction = _broken_reaction
    
    if reaction:
        print("属性反应：", AttributeReaction.get_type_str(type))
        reaction.effect(info)
        triggered_reaction.emit(info, type)
        FloatTextSystem.show_reaction_text(info, type)
        if reaction.is_stop_anomaly: state.stop()


# # 设置节点配置警告，脚本需是工具脚本
# func _get_configuration_warnings() -> PackedStringArray:
#     var warnings = PackedStringArray()

#     if get_scene_file_path() == "":
#         warnings.append("请从保存的场景实例化该节点，以确保正常工作")

#     return warnings
